package com.randi.generator.controller;

import com.randi.common.core.domain.Result;
import com.randi.common.enums.ResultCode;
import com.randi.generator.entity.GenTable;
import com.randi.generator.service.IDbTableService;
import com.randi.generator.service.IGenTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ziye
 */
@RestController
@RequestMapping("/tool/generate")
@Api(tags = "生成代码")
public class GenController {

    @Resource
    private IGenTableService iGenTableService;

    @Resource
    private IDbTableService iDbTableService;

    /**
     * 获取列表
     *
     * @param genTable genTable
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    public Result list(@RequestBody GenTable genTable) {
        List<GenTable> genTables = iGenTableService.selectGenTableList(genTable);
        return Result.success().put(genTables);
    }

    /**
     * 查看详情
     *
     * @param tableId tableId
     * @return Result
     */
    @GetMapping("/detail/{tableId}")
    @ApiOperation(value = "查看详情", notes = "")
    public Result selectGenTableById(@PathVariable Long tableId) {
        GenTable genTable = iGenTableService.selectGenTableById(tableId);
        return Result.success().put(genTable);
    }

    /**
     * 导入数据
     *
     * @param tableNames tableNames
     * @return status
     */
    @PostMapping("/import")
    @ApiOperation(value = "导入数据", notes = "")
    public Result importGenTable(@RequestBody String[] tableNames) {
        int status = iGenTableService.importGenTable(tableNames);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_IMPORT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_IMPORT_SUCCESS);
    }


    /**
     * 更新数据
     *
     * @param genTable 业务信息
     * @return 结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新数据", notes = "")
    public Result updateGenTable(@RequestBody GenTable genTable) {
        int status = iGenTableService.updateGenTable(genTable);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_EDITOR_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param tableId genTableId
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除数据", notes = "")
    public Result deleteGenTable(@RequestBody Long tableId) {
        int status = iGenTableService.deleteGenTable(tableId);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

    /**
     * 批量删除数据
     *
     * @param tableIds tableIds
     * @return 结果
     */
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除", notes = "")
    public Result deleteBatchGenTable(@RequestBody List<Long> tableIds) {
        int status = iGenTableService.deleteBatchGenTable(tableIds);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }


    /* ---------------------------  数据库操作  -------------------------- */

    /**
     * 获取导入列表
     *
     * @param genTable genTable
     * @return 结果
     */
    @PostMapping("/db/list")
    @ApiOperation(value = "获取导入列表", notes = "")
    public Result selectDbTableList(@RequestBody GenTable genTable) {
        List<GenTable> genTables = iDbTableService.selectDbTableList(genTable);

        return Result.success().put(genTables);
    }

    /* ---------------------------  代码生成操作  -------------------------- */

    /**
     * 预览代码
     *
     * @param tableId tableId
     * @return Result
     */
    @GetMapping("/preview/{tableId}")
    @ApiOperation(value = "预览代码", notes = "")
    public Result previewCode(@PathVariable Long tableId) {
        Map<String, String> dataMap = iGenTableService.previewCode(tableId);
        return Result.success().put(dataMap);
    }

    /**
     * 生成代码
     *
     * @param tableId tableId
     * @return Result
     */
    @GetMapping("/download/{tableId}")
    @ApiOperation(value = "生成代码", notes = "")
    public void downloadCode(HttpServletResponse response, @PathVariable Long tableId) throws IOException {

        byte[] data = iGenTableService.downloadCode(tableId);
        this.genZipResponse(response, data);

    }

    /**
     * 生成zip文件
     */
    private void genZipResponse(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"randi.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

}
